package com.exam.utils;

import java.sql.*;
import java.util.ResourceBundle;

public class JdbcControl extends SsqlConnection {
    private static Connection conn;
    private static Statement stmt;
    private static ResultSet rs;


    public static Connection getConn() {
        return conn;
    }

    public static void setConn(Connection conn) {
        JdbcControl.conn = conn;
    }

    public static Statement getStmt() {
        return stmt;
    }

    public static void setStmt(Statement stmt) {
        JdbcControl.stmt = stmt;
    }

    public static ResultSet getRs() {
        return rs;
    }

    public static void setRs(ResultSet rs) {
        JdbcControl.rs = rs;
    }

    static{
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
        ResourceBundle bundle= ResourceBundle.getBundle("jdbc");
        String url=bundle.getString("url");
        String user=bundle.getString("user");
        String password=bundle.getString("password");
        return  DriverManager.getConnection(url,user,password);
    }

    public static int insert(String insertThing,String table,String values) throws SQLException {
        String sql=sqlConnectInsert(insertThing,table,values);
        return JdbcControl.getStmt().executeUpdate(sql);
    }
    public static void returnSetRs(String selectThing,String table,String condiction) throws SQLException {
        String sql= JdbcControl.sqlConnectSelect(selectThing,table,condiction);
        JdbcControl.setRs(JdbcControl.getStmt().executeQuery(sql));
    }
    public static int update(String table,String needSet,String valueString,String condiction) throws SQLException {
        String sql=sqlConnectUpdate(table,needSet,valueString,condiction);
        return JdbcControl.getStmt().executeUpdate(sql);
    }
    public static int selectReturnInt(String table,String selectThing,String condiction) throws SQLException {
        String sql=sqlConnectSelect(selectThing,table,condiction);
        JdbcControl.setRs(JdbcControl.getStmt().executeQuery(sql));
        while(JdbcControl.getRs().next()){
            return JdbcControl.getRs().getInt(selectThing);
        }
        return 0;
    }
    public static String selectReturnString(String table,String selectThing,String condiction) throws SQLException {
        String sql=sqlConnectSelect(selectThing,table,condiction);
        JdbcControl.setRs(JdbcControl.getStmt().executeQuery(sql));
        while(JdbcControl.getRs().next()){
            return JdbcControl.getRs().getString(selectThing);
        }
        return "0";
    }
    public static void close() throws SQLException {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        if(rs!=null){
            try{
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
